package kr.hkit.powerjava.coffee;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;

import kr.hkit.powerjava.coffee.dao.CoffeeDAO;
import kr.hkit.powerjava.coffee.db.ConnectionFactory;
import kr.hkit.powerjava.coffee.dto.SalesRecord;

public class FrameEx extends JFrame {

	private JTabbedPane contentPane;
	private PanelInput panel;
	private PanelOutput panel_1;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	Connection connnection = ConnectionFactory.getConnection();
	CoffeeDAO dao = new CoffeeDAO(connnection);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameEx frame = new FrameEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameEx() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 228, 318);
		contentPane = new JTabbedPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		panel = new PanelInput();
		panel.getCbpdCode().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dao.getName(((String)panel.getCbpdCode().getSelectedItem()));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		contentPane.addTab("New tab", null, panel, null);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		btnNewButton = new JButton("입력");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String code = (String) panel.getCbpdCode().getSelectedItem();
				int price = Integer.parseInt(panel.getPdPrice().getTextField()
						.getText().trim());
				int amount = Integer.parseInt(panel.getAmount().getTextField()
						.getText().trim());
				double marginrate = Double.parseDouble(panel.getMarjinRate()
						.getTextField().getText().trim());
				try {
					dao.insertItem(new SalesRecord(code, price, amount, marginrate));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		panel.add(btnNewButton);

		btnNewButton_1 = new JButton("출력1");
		panel.add(btnNewButton_1);

		btnNewButton_2 = new JButton("출력2");
		panel.add(btnNewButton_2);

		panel_1 = new PanelOutput((String) null);
		contentPane.addTab("New tab", null, panel_1, null);
	}
}
